package com.ttai.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.ttai.R;
import com.ttai.model.net.VerifyAccountBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class VerifyLoginRecycleViewAdapter extends RecyclerView.Adapter {
    public Context context;
    //    选择checkbox的个数
    public int checkamount = 0;
    public ArrayList<VerifyAccountBean.SellectListBean> accountArrayList = new ArrayList<>();
    public VerifyAccountBean.SellectListBean sellectListBean;
    public SparseBooleanArray mSelectedPositions = new SparseBooleanArray();
    public ArrayList<String> checkedList = new ArrayList<>();

    private String data = "";

    public SparseBooleanArray getmSelectedPositions() {
        return mSelectedPositions;
    }

    public void setmSelectedPositions(SparseBooleanArray mSelectedPositions) {
        this.mSelectedPositions = mSelectedPositions;
    }


    public VerifyLoginRecycleViewAdapter(Context context, ArrayList<VerifyAccountBean.SellectListBean> accountArrayList) {
        this.context = context;
        this.accountArrayList = accountArrayList;
    }


    //根据位置设置条目的状态（添加）
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    private void deleteItemCheck(int position) {
        mSelectedPositions.delete(position);
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }

    public String getData() {
        for (int i = 0; i < checkedList.size(); i++) {
            data = data + checkedList.get(i) + "#";
        }
        return data;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_verifylogin, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
//        sellectListBean = accountArrayList.get(position);
        Picasso.with(context)
                .load(accountArrayList.get(position).getIcon())
                .resize(150,150)
                .placeholder(R.drawable.taisecuritylogosmall)
                .into(((ViewHolder) holder).ivVerifyloginicon);

        ((ViewHolder) holder).tvVerifyloginicon.setText(accountArrayList.get(position).getAccountname());
        ((ViewHolder) holder).cbCheck.setChecked(isItemChecked(position));
        ((ViewHolder) holder).cbCheck.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (checkamount > 2) {
                    ((ViewHolder) holder).cbCheck.setClickable(false);
                    ((ViewHolder) holder).cbCheck.setEnabled(false);
                } else {
                    if (isItemChecked(position)) {
                        setItemChecked(position, false);
                        checkedList.remove(accountArrayList.get(position).getAccountname());
                        checkamount--;
                    } else {
                        setItemChecked(position, true);
                        checkedList.add(accountArrayList.get(position).getAccountname());
                        checkamount++;
                    }
                    Toast.makeText(context, "已选择" + checkedList.size() + "项,最多选择三项", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return accountArrayList == null ? 0 : accountArrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_verifylogintext)
        TextView tvVerifyloginicon;
        @Bind(R.id.cb_check)
        CheckBox cbCheck;
        @Bind(R.id.iv_verifyloginicon)
        ImageView ivVerifyloginicon;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);

        }
    }
}
